# request_util.py

import requests
from requests.exceptions import RequestException

class RequestUtil:
    @staticmethod
    def get(url, params=None, headers=None, timeout=10):
        """
        发送GET请求
        :param url: 请求的URL
        :param params: 请求参数
        :param headers: 请求头
        :param timeout: 超时时间
        :return: 请求的响应
        """
        try:
            response = requests.get(url, params=params, headers=headers, timeout=timeout)
            response.raise_for_status()  # 如果响应状态码不是200，将抛出异常
            return response
        except RequestException as e:
            print(f"请求失败: {e}")
            return None

    @staticmethod
    def post(url, data=None, json=None, headers=None, timeout=10):
        """
        发送POST请求
        :param url: 请求的URL
        :param data: 表单数据
        :param json: JSON数据
        :param headers: 请求头
        :param timeout: 超时时间
        :return: 请求的响应
        """
        try:
            response = requests.post(url, data=data, json=json, headers=headers, timeout=timeout)
            response.raise_for_status()  # 如果响应状态码不是200，将抛出异常
            return response
        except RequestException as e:
            print(f"请求失败: {e}")
            return None

# 使用示例
if __name__ == "__main__":
    util = RequestUtil()
    response_get = util.get("http://httpbin.org/get", params={"key": "value"})
    if response_get:
        print(response_get.text)  # 打印响应内容

    response_post = util.post("http://httpbin.org/post", json={"key": "value"})
    if response_post:
        print(response_post.text)  # 打印响应内容